package com.juyu.miliao.api.controller.whitepacket;

import com.juyu.miliao.api.domain.dto.CommentReplyInfoDTO;
import com.juyu.miliao.api.domain.dto.WhitePacketCommentReplyInfoDTO;
import com.juyu.miliao.api.service.CommentReplyInfoService;
import com.juyu.miliao.api.service.DynamicCommentInfoService;
import com.juyu.miliao.api.service.whitepacket.WhitePacketCommentReplyInfoService;
import com.juyu.miliao.common.domain.Subject;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 回复controller
 *
 * @author xuanqicheng
 * @create 2019-21-15:31
 */
@RestController
@RequestMapping("/packetReply")
public class WhitePacketCommentReplyInfoController {

    @Autowired
    private WhitePacketCommentReplyInfoService commentReplyInfoService;


    /**
     * 新增回复评论，目前是二级评论
     * @param token
     * @param commentReplyInfoDTO
     * @param bindingResult
     */
    @PostMapping("/")
    public void addCommentReply(Authentication token, @Validated WhitePacketCommentReplyInfoDTO commentReplyInfoDTO,
                                BindingResult bindingResult){

        if(bindingResult.hasErrors()){
            throw  new ConsumeRuntimeException(bindingResult.getFieldError().getDefaultMessage());
        }

        Subject subject = (Subject) token.getPrincipal();

        commentReplyInfoDTO.setReplyFromUserId(subject.getId());

        commentReplyInfoService.addCommentReply(commentReplyInfoDTO);

    }
}
